package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.PrescriptionMouldDetailsAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.PrescriptionMouldDetails;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;


public class PrescriptionMouldDetailsActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.yizu)
    TextView yizu;
    private RecyclerView recyclerView;
    private String temp_id;
    private PrescriptionMouldDetailsAdapter adapter;
    private List<PrescriptionMouldDetails> lists;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_prescription_mould_details;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        temp_id = getIntent().getStringExtra("temp_id");

        recyclerView = findViewById(R.id.recyclerView);
        lists = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new PrescriptionMouldDetailsAdapter(mActivity);
        recyclerView.setAdapter(adapter);
        getMakeTemp();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("data");

                        Gson gson = new Gson();
                        for (int i = 0; i < array.length(); i++) {

                            PrescriptionMouldDetails pd = gson.fromJson(array.getJSONObject(i).toString(), PrescriptionMouldDetails.class);

                            if (i==0){
                                title.setText(pd.getTitle());
                                yizu.setText(pd.getAdvice());
                            }
                            lists.add(pd);

                        }
                        adapter.setListAndNotifyDataSetChanged(lists);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getMakeTemp() {

        List<String> l = new ArrayList<>();
        l.add(temp_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getMakeDetailed");
        params.put("data[make_id]", temp_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.make/entry", params, handler, 1, 404);

    }

}
